package com.gxa.setting.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.common.base.RespResultCode;
import com.gxa.setting.dto.AddLabelsDto;
import com.gxa.setting.dto.ServiceLabelDTO;
import com.gxa.setting.dto.UpdateLabelsDto;
import com.gxa.setting.entity.SetLabel;
import com.gxa.setting.mapper.SetLabelMapper;
import com.gxa.setting.service.SetLabelService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.data.redis.connection.stream.ByteRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetLabelServiceImpl implements SetLabelService {

    @Resource
    private SetLabelMapper setLabelMapper;

    int i;

    @Override
    public RespResult serviceList(Integer currentPage, Integer pageSize) {

        if (currentPage != null && pageSize != null) {
            Page<ServiceLabelDTO> page = new Page<>();
            List<ServiceLabelDTO> list = setLabelMapper.serviceList(page);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("page", page);
            return RespResult.success(map);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(), null);
    }

    @Override
    public RespResult addLabelsByServiceId(AddLabelsDto addLabelsDto) {

        if (addLabelsDto != null){
            String[] labels = addLabelsDto.getLabels();
            Integer serviceId = addLabelsDto.getServiceId();
            for(String name:labels){
                setLabelMapper.addLabelsByServiceId(serviceId,name);
            }
            return RespResult.success("添加成功");
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(), null);
    }

    @Override
    public RespResult updateLabelSByServiceIdAndLabelIds(UpdateLabelsDto updateLabelsDto) {

        if (updateLabelsDto != null){
            Integer serviceId = updateLabelsDto.getServiceId();
            Integer[] labelIds = updateLabelsDto.getLabelIds();
            String[] labelNames = updateLabelsDto.getLabelNames();
            Map idMap = new HashMap();
//            if (labelIds.length > 0 && serviceId != null) {
//                for(i = 0;i < labelIds.length;i++){
//                    String name = labelNames[i];
//                    Integer id = labelIds[i];
//                    setLabelMapper.updateLabelSByServiceIdAndLabelIds(serviceId,id,name);
//                }
//                return RespResult.success("修改成功");
//            }
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(), null);
    }

    @Override
    public RespResult queryLabelListByServiceId(Integer serviceId) {
        if (serviceId != null){
            List<SetLabel> list =  setLabelMapper.queryLabelListByServiceId(serviceId);
            ServiceLabelDTO serviceLabelDTO = setLabelMapper.queryServiceByServiceId(serviceId);
            Map map = new HashMap();
            map.put("list",list);
            map.put("serviceLabelDTO",serviceLabelDTO);
            return RespResult.success(map);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(), null);
    }

    @Override
    public RespResult updateLabelById(Integer id) {

        if (id != null){
            SetLabel setLabel =  setLabelMapper.queryLabel(id);
            Byte getStatus = setLabel.getStatus();
            Byte status;
            if (getStatus>0){
                status = 0;
            }else{
                status = 1;
            }
            setLabelMapper.updateStatus(id,status);
            return RespResult.success("修改成功");
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(), null);
    }

}
